﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using System.Windows.Forms;

namespace YanZhiwei.DotNet2.Utilities.WinForm
{
    /// <summary>
    /// ComboBox 帮助类
    /// </summary>
    public static class ComboBoxHelper
    {
        #region  为ComboBox绑定数据源并提供下拉提示
        /// <summary>
        ///  为ComboBox绑定数据源并提供下拉提示
        /// </summary>
        /// <typeparam name="T">泛型</typeparam>
        /// <param name="combox">ComboBox</param>
        /// <param name="list">数据源</param>
        /// <param name="displayMember">显示字段</param>
        /// <param name="valueMember">隐式字段</param>
        /// <param name="displayText">下拉提示文字</param>
        public static void Bind<T>(this ComboBox combox, IList<T> list, string displayMember, string valueMember, string displayText)
        {
            AddItem(list, displayMember, displayText);
            combox.DataSource = list;
            combox.DisplayMember = displayMember;
            if (!string.IsNullOrEmpty(valueMember))
                combox.ValueMember = valueMember;
        }
        private static void AddItem<T>(IList<T> list, string displayMember, string displayText)
        {
            Object _obj = Activator.CreateInstance<T>();
            Type _type = _obj.GetType();
            if (!string.IsNullOrEmpty(displayMember))
            {
                PropertyInfo _displayProperty = _type.GetProperty(displayMember);
                _displayProperty.SetValue(_obj, displayText, null);
            }
            list.Insert(0, (T)_obj);
        }
        #endregion
        #region 绑定ComboBox【线程安全】
        /// <summary>
        /// 绑定ComboBox【线程安全】
        /// </summary>
        /// <typeparam name="T">泛型</typeparam>
        /// <param name="combox">ComboBox</param>
        /// <param name="data">集合</param>
        /// <param name="displayMember">显示字段名称</param>
        /// <param name="valueMember">隐式子段名称</param>
        public static void BindCombox<T>(this ComboBox combox, IList<T> data, string displayMember, string valueMember) where T : class
        {
            combox.UIThread((MethodInvoker)delegate
            {
                combox.DataSource = data;
                combox.DisplayMember = displayMember;
                if (!string.IsNullOrEmpty(valueMember))
                    combox.ValueMember = valueMember;
            });
        }
        #endregion 
        #region 获取选中索引【线程安全】
        /// <summary>
        /// 获取选中索引【线程安全】
        /// </summary>
        /// <param name="combox">ComboBox</param>
        /// <returns>选中索引</returns>
        public static int GetSelectedIndexTF(this ComboBox combox)
        {
            int _selectedIndex = -1;
            combox.UIThread((MethodInvoker)delegate
            {
                _selectedIndex = combox.SelectedIndex;
            });
            return _selectedIndex;
        }
        #endregion 
        #region 设置选中索引，并且设置是否可用 【线程安全】
        /// <summary>
        /// 设置选中索引，并且设置是否可用 【线程安全】
        /// </summary>
        /// <param name="cb">ComboBox</param>
        /// <param name="index">索引</param>
        /// <param name="enable">Enabled</param>
        public static void OpComboBoxTF(this ComboBox cb, int index, bool enable)
        {
            cb.UIThread((MethodInvoker)delegate
            {
                cb.SelectedIndex = index;
                cb.Enabled = enable;
            });
        }
        #endregion 
        #region 设置选中索引【线程安全】
        /// <summary>
        /// 设置选中索引【线程安全】
        /// </summary>
        /// <param name="cb">ComboBox</param>
        /// <param name="index">索引</param>
        public static void OpComboBoxTF(this ComboBox cb, int index)
        {
            cb.UIThread((MethodInvoker)delegate
            {
                cb.SelectedIndex = index;
            });
        }
        #endregion 
    }
}
